'use strict';

App.controller('orderController', ['$rootScope', '$scope', 'Page', function($rootScope, $scope, Page) {
	//获取全部方案信息
	 Page.ajaxGet('linePlan/getAllLinePlan.do', null, function(resp) {
	        $scope.linePlanList = resp.data;
	    });
	 
	$scope.orderGrid = {
		 column_defs: [
		   	        {
		   	            target: 10,
		   	            buttons: [
		   	                {
		   	                    name: '订单详情',
		   	                    class: 'btn purple',
		   	                    icon: 'fa-cog',
		   	                    event: 'showDetail'
		   	                },{
		   	                    name: '手动退款',// 针对支付成功的订单
		   	                    class: 'btn btn-success',
		   	                    icon: 'fa-gavel',
		   	                    event: 'showRefundModal',
		   	                    visiable: function(row) {
		                         	if(row.orderStatus=='3'){
		                         		 return true;
		                         	}else {
		                         		 return false;
		                         	}
		                         }
		   	                },{
		   	                    name: '取消订单',// 针对未支付的订单
		   	                    class: 'btn btn-danger',
		   	                    icon: 'fa-mail-reply',
		   	                    event: 'cancelOrder',
		   	                    visiable: function(row) {
		                         	if(row.orderStatus=='1'){
		                         		 return true;
		                         	}else {
		                         		 return false;
		                         	}
		                         }
		   	                }
		   	            ]
		   	        }
		       	],
       	showRefundModal: function(row) {// 手动退款
	       	 var param = {};
			 $scope.order = row;
			 param.orderId = row.orderId;
			 Page.ajaxPost('order/canBackDetails.do', param, function (resp) {
				 $scope.orderDetailList = resp.data.passengers;
				 $scope.refundModal.show();
	            });
		},
		cancelOrder: function(row) {// 未支付的订单直接整个订单关闭
			 var param = {};
			 param.orderId = row.orderId;
			 Page.ajaxPost('order/cancel.do', param, function (resp) {
	                Page.notice('订单取消成功!');
	                $scope.orderGrid.query();
	            });
		},
		showDetail : function(row){
			 var param = {};
			 $scope.order = row;
			 param.orderId = row.orderId;
			 param.orderId = row.orderId;
			 Page.ajaxPost('order/details.do', param, function (resp) {
				 $scope.orderDetailList = resp.data.passengers;
				 $scope.detailModal.show();
	            });
		},
		refund: function() {// 申请退款
			var needData = {};
			needData.orderId =  $scope.order.orderId;
			needData.reason =  "手动退款测试";
			needData.orderBackType = "2";
			needData.backInfos = [];
			var hasCheckedDate = false;
			for(var i=0;i<$scope.orderDetailList.length;i++ ){
				var backInfo = {};
				var orderDetail = $scope.orderDetailList[i];
				backInfo.passengerPhone = orderDetail.passengerPhone
				backInfo.passengerName = orderDetail.passengerName
				backInfo.dates = [];
				 $('input[name="'+orderDetail.passengerPhone+'"]:checked').each(function(){    
					 var date = {};
					 date.date = $(this).val();
					 backInfo.dates.push(date);    
					 hasCheckedDate = true;
					  });   
				 needData.backInfos.push(backInfo);
			}
			if(!hasCheckedDate){
				Page.alert("请选择退款日期!");
			}else {
				Page.ajaxPost('orderBack/apply.do', needData, function (resp) {
				 	Page.notice('手动退款成功!');
				 	 $scope.refundModal.hide();
	                $scope.orderGrid.query();
	            });
			}
			 
		},
	};
}]);

